package com.bobo.controller.front;

import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.model.Photo;
import com.bobo.model.User;
import com.bobo.service.user.IUserService;
import com.bobo.service.user.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by anting on 16/11/6.
 */

//    192.168.0.106:8080/login/login
@WebServlet(name = "login",urlPatterns = {"/login/*"})
public class Login extends BaseServlet {

    private IUserService userService = new UserServiceImpl();

    @RequestMapping("/login")
    public String loginn(HttpServletRequest request) {

        String link = request.getParameter("link");

        if(link == null) {
            request.getSession().setAttribute("tempLink", "/index/blog");
        } else {
            request.getSession().setAttribute("tempLink", link);
        }



        return "jsp/front/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request) {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String link = (String)request.getSession().getAttribute("tempLink");


        if("".equals(name.trim()) || "".equals(pwd.trim())){
            return "error";
        }
        User user = userService.userCompare(name, pwd);
        if (user.getName() != null) {
            if (user.getName().equals(name)) {
                request.getSession().setAttribute("user", user);

                return "redirect:" + link;
            }

        }
        return "error";
    }


    @RequestMapping("/register")
    public String add1(HttpServletRequest request) {
        return "jsp/front/user/add";

    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String pwd1 = request.getParameter("pwd1");
        String pwd2 = request.getParameter("pwd2");
        int sex = Integer.parseInt(request.getParameter("sex"));
        String birthday = request.getParameter("birthday");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address = request.getParameter("address");

        User user = new User();
        Photo photo = new Photo();
        photo.setId(1);
        user.setPhoto(photo);
        user.setName(name);
        user.setSex(sex);
        user.setPhone(phone);
        user.setEmail(email);
        user.setAddress(address);

        if (pwd1.equals(pwd2)){
            user.setPassword(pwd1);
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(birthday);
                java.sql.Date sqldate = new java.sql.Date(date.getTime());
                user.setBirthday(sqldate);
                userService.create(user);
                return "redirect:/user/login";
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "error";

    }

}
